#!/bin/sh
#
# ucarp-multi		Startup script for several instances of the ucarp VIP system.
#
# Version:		@(#)ucarp-multi.rc 0.20 30-01-2010 f.duchene@car-online.fr
#
### BEGIN INIT INFO
# Provides:             ucarp-multi
# Required-Start:       $syslog
# Required-Stop:        $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    UCARP-Multi - failover IP
### END INIT INFO


CONFIG__MAX_NUM_OF_STOP_TRIES=4
CONFIG__SLEEP_TIME_BTWN_OPERATIONS=1
UCARP_BIN='/usr/sbin/ucarp'
UCARP_DIR_CFG='/etc/ucarp-multi'
UCARP_DIR_CONF_EN="${UCARP_DIR_CFG}/vip-enabled"


# some basic checks 
[ ! -x $UCARP_BIN ] && echo "ERROR - $UCARP_BIN doesn't exist or not executable !" && exit 1




# for each enabled interface 
if [ ! -d ${UCARP_DIR_CONF_EN} ] ; then
	echo "the ucarp-multi vip-enabled directory '${UCARP_DIR_CONF_EN}'does not exist"
	exit 1
fi

ls ${UCARP_DIR_CONF_EN}/*.conf
RES=$?
if [ $RES -ne 0 ] ; then
	echo "No vip is enabled in ${UCARP_DIR_CONF_EN}. If you want to enable one, please create symbolic link from vip-available/*.conf in vip-enabled , in the directory ${UCARP_DIR_CFG}"
	exit 1
fi



for file in ${UCARP_DIR_CONF_EN}/*.conf ; do
	echo "Loading configuration file: $file"
	UCARP_CONF_FILE=$file
	[ ! -x "$UCARP_CONF_FILE" ] && echo "ERROR - $UCARP_CONF_FILE doesn't exist or not executable !" && exit 1
	. $UCARP_CONF_FILE

	
	CONF__PHYSICAL_INTERFACE=$UCARP_INTERFACE
	
	CONF__SUB_IF_NUMBER=`basename $file | sed 's/\\.conf//g' | sed 's/.*_//g'`

	#echo $CONF__SUB_IF_NUMBER
	#CONF__PHYSICAL_INTERFACE="eth0"
	#CONF__SUB_IF_NUMBER=1



SUB_INTERFACE="${CONF__PHYSICAL_INTERFACE}:${CONF__SUB_IF_NUMBER}"
SUB_IF_STRING="${CONF__PHYSICAL_INTERFACE}_${CONF__SUB_IF_NUMBER}"

UCARP_PID_FILE="/var/run/ucarp_${SUB_IF_STRING}.pid"
#UCARP_CONF_FILE="/etc/ucarp/${SUB_IF_STRING}/ucarp.conf"



case "$1" in
	start)
		echo "Starting UCARP as a daemon with $UCARP_CONF_FILE as configuration file..."

		[ -f $UCARP_PID_FILE ] && echo "ERROR - $UCARP_PID_FILE exists, maybe ucarp is still running" && exit 1

		if [ "$UCARP_MASTER" = 'yes' ]
		then
			[ $DEBUG_VALUE -ge 1 ] && echo "DEBUG - $UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -P -B"
			$UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -P -B
		else
			[ $DEBUG_VALUE -ge 1 ] && echo "DEBUG - $UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -B -k $UCARP_ADVSKEW"
			$UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -B -k $UCARP_ADVSKEW
		fi

		[ $? -ne 0 ] && echo "Starting UCARP as a daemon...FAILED" && exit 1
		pidof -s "$UCARP_BIN" > $UCARP_PID_FILE
		chmod 700 $UCARP_PID_FILE
		echo "Starting UCARP as a daemon (pid : `cat $UCARP_PID_FILE`)...OK"
	;;
	start-nodaemon)
		echo "Starting UCARP with $UCARP_CONF_FILE as configuration file..."
		if [ "$UCARP_MASTER" = 'yes' ]
		then
			[ $DEBUG_VALUE -ge 1 ] && echo "DEBUG - $UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -P"
			$UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -P
		else
			[ $DEBUG_VALUE -ge 1 ] && echo "DEBUG - $UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -k $UCARP_ADVSKEW"
			$UCARP_BIN -i $UCARP_INTERFACE -b $UCARP_ADV -s $UCARP_ADDR_PHY -v $UCARP_VID -o $UCARP_PASSWD_FILE -a $UCARP_ADDR_VIR -u $UCARP_UP_INT_SCRIPT -d $UCARP_DOWN_INT_SCRIPT -z -k $UCARP_ADVSKEW
		fi
	;;
	stop)
		echo "Stopping UCARP daemon for sub-interface $SUB_INTERFACE"
		if [ -f "$UCARP_PID_FILE" ]
		then
			DAEMON_PID=`cat $UCARP_PID_FILE`
			echo "(pid : `cat $UCARP_PID_FILE`)"
			
			i=0
			stop=""
			while [ $i -lt $CONFIG__MAX_NUM_OF_STOP_TRIES ] && [ -z "$stop" ]
			do
				i=$[$i+1]
				echo "try: $i"
				kill -15 $DAEMON_PID
				sleep ${CONFIG__SLEEP_TIME_BTWN_OPERATIONS}
				rm -f $UCARP_PID_FILE

				if [ -f "$UCARP_PID_FILE" ]
				then
					echo "error"
				else
					echo "OK"
					stop=1
				fi
			done
		else
			echo "not running"
		fi
	;;
	status)
		if [ -f "$UCARP_PID_FILE" ]
		then
			echo "UCARP is running (pid : `cat $UCARP_PID_FILE`)"
		else
			echo "UCARP is not running"
		fi
	;;
	restart|force-reload)
		$0 stop
		sleep 5
		$0 start
	;;
	*)
		echo "Usage: $0 {start|start-nodaemon|stop|status|restart|force-reload}"
		exit 1
	;;
	esac

done


exit 0

